#include "interface.h"
#include "client_protocol.h"

static GnomeUIInfo file3_menu_uiinfo[] =
{
  GNOMEUIINFO_MENU_EXIT_ITEM (gtk_main_quit, NULL),
  GNOMEUIINFO_END
};

static GnomeUIInfo help3_menu_uiinfo[] =
{
  GNOMEUIINFO_HELP ("w3live"),
  GNOMEUIINFO_MENU_ABOUT_ITEM (showAbout, NULL),
  GNOMEUIINFO_END
};

static GnomeUIInfo menubarConn_uiinfo[] =
{
  GNOMEUIINFO_MENU_FILE_TREE (file3_menu_uiinfo),
  GNOMEUIINFO_MENU_HELP_TREE (help3_menu_uiinfo),
  GNOMEUIINFO_END
};

GtkWidget*
create_connect (void)
{
  GtkWidget *connect;
  GtkWidget *bonobodock3;
  GtkWidget *vboxConn;
  GtkWidget *tableConn;
  GtkWidget *labelHost;
  GtkWidget *labelPort;
  GtkWidget *entryPort;
  GtkWidget *entryHost;
  GtkWidget *hbuttonboxConn;
  GtkWidget *buttonQuit;
  GtkWidget *buttonLaunch;
  GtkWidget *alignment1;
  GtkWidget *hbox2;
  GtkWidget *image1;
  GtkWidget *labelLaunch;
  GtkWidget *buttonConnect;
  GtkWidget *alignment2;
  GtkWidget *hbox3;
  GtkWidget *image2;
  GtkWidget *labelConnect;
  GtkTooltips *tooltips;

  gchar *value;
  GConfEngine *gc_engine = gconf_engine_get_default();
  
  tooltips = gtk_tooltips_new ();

  connect = gnome_app_new ("W3live", "w3live - connect");

  bonobodock3 = GNOME_APP (connect)->dock;
  gtk_widget_show (bonobodock3);

  gnome_app_create_menus (GNOME_APP (connect), menubarConn_uiinfo);

  vboxConn = gtk_vbox_new (FALSE, 4);
  gtk_widget_show (vboxConn);
  gnome_app_set_contents (GNOME_APP (connect), vboxConn);

  tableConn = gtk_table_new (2, 2, FALSE);
  gtk_widget_show (tableConn);
  gtk_box_pack_start (GTK_BOX (vboxConn), tableConn, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (tableConn), 10);
  gtk_table_set_row_spacings (GTK_TABLE (tableConn), 5);
  gtk_table_set_col_spacings (GTK_TABLE (tableConn), 42);

  labelHost = gtk_label_new ("Host:");
  gtk_widget_show (labelHost);
  gtk_table_attach (GTK_TABLE (tableConn), labelHost, 0, 1, 0, 1,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelHost), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (labelHost), 0, 0.5);

  labelPort = gtk_label_new ("Port:");
  gtk_widget_show (labelPort);
  gtk_table_attach (GTK_TABLE (tableConn), labelPort, 0, 1, 1, 2,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelPort), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (labelPort), 0, 0.5);

  entryPort = gtk_entry_new ();
  gtk_widget_show (entryPort);
  gtk_table_attach (GTK_TABLE (tableConn), entryPort, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_SHRINK),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_tooltips_set_tip (tooltips, entryPort, "Port which the w3live daemon is running on", NULL);

  if((value = gconf_engine_get_string(gc_engine,"/apps/w3live/port",NULL))) {
    gtk_entry_set_text(GTK_ENTRY (entryPort),value);
    g_free(value);
  }
  else
     gtk_entry_set_text (GTK_ENTRY (entryPort), "1030");

  gtk_entry_set_width_chars (GTK_ENTRY (entryPort), 35);

  entryHost = gtk_entry_new ();
  gtk_widget_show (entryHost);
  gtk_table_attach (GTK_TABLE (tableConn), entryHost, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_SHRINK),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_tooltips_set_tip (tooltips, entryHost, "Host where w3live daemon is running", NULL);

  if((value = gconf_engine_get_string(gc_engine,"/apps/w3live/host",NULL))) {
    gtk_entry_set_text(GTK_ENTRY (entryHost),value);
    g_free(value);
  }
  else
    gtk_entry_set_text (GTK_ENTRY (entryHost), "127.0.0.1");
  
  gtk_entry_set_width_chars (GTK_ENTRY (entryHost), 35);

  hbuttonboxConn = gtk_hbutton_box_new ();
  gtk_widget_show (hbuttonboxConn);
  gtk_box_pack_end (GTK_BOX (vboxConn), hbuttonboxConn, FALSE, TRUE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (hbuttonboxConn), 10);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonboxConn), GTK_BUTTONBOX_END);
  gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonboxConn), 5);

  buttonQuit = gtk_button_new_from_stock ("gtk-quit");
  gtk_widget_show (buttonQuit);
  gtk_container_add (GTK_CONTAINER (hbuttonboxConn), buttonQuit);
  GTK_WIDGET_SET_FLAGS (buttonQuit, GTK_CAN_DEFAULT);
  gtk_tooltips_set_tip (tooltips, buttonQuit, "Quit", NULL);

  buttonLaunch = gtk_button_new ();
  gtk_widget_show (buttonLaunch);
  gtk_container_add (GTK_CONTAINER (hbuttonboxConn), buttonLaunch);
  GTK_WIDGET_SET_FLAGS (buttonLaunch, GTK_CAN_DEFAULT);
  gtk_tooltips_set_tip (tooltips, buttonLaunch, "Start a new w3live daemon", NULL);

  alignment1 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment1);
  gtk_container_add (GTK_CONTAINER (buttonLaunch), alignment1);

  hbox2 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox2);
  gtk_container_add (GTK_CONTAINER (alignment1), hbox2);

  image1 = gtk_image_new_from_stock ("gtk-execute", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image1);
  gtk_box_pack_start (GTK_BOX (hbox2), image1, FALSE, FALSE, 0);

  labelLaunch = gtk_label_new_with_mnemonic ("Start New");
  gtk_widget_show (labelLaunch);
  gtk_box_pack_start (GTK_BOX (hbox2), labelLaunch, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (labelLaunch), GTK_JUSTIFY_LEFT);

  buttonConnect = gtk_button_new ();
  gtk_widget_show (buttonConnect);
  gtk_container_add (GTK_CONTAINER (hbuttonboxConn), buttonConnect);
  GTK_WIDGET_SET_FLAGS (buttonConnect, GTK_CAN_DEFAULT);
  gtk_tooltips_set_tip (tooltips, buttonConnect, "Connect to the w3live daemon", NULL);

  alignment2 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment2);
  gtk_container_add (GTK_CONTAINER (buttonConnect), alignment2);

  hbox3 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox3);
  gtk_container_add (GTK_CONTAINER (alignment2), hbox3);

  image2 = gtk_image_new_from_stock ("gtk-ok", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image2);
  gtk_box_pack_start (GTK_BOX (hbox3), image2, FALSE, FALSE, 0);

  labelConnect = gtk_label_new_with_mnemonic ("Connect");
  gtk_widget_show (labelConnect);
  gtk_box_pack_start (GTK_BOX (hbox3), labelConnect, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (labelConnect), GTK_JUSTIFY_LEFT);

  gtk_signal_connect_object (GTK_OBJECT (buttonQuit), "clicked",
                             GTK_SIGNAL_FUNC (gtk_main_quit),
                             GTK_OBJECT (NULL));
  gtk_signal_connect        (GTK_OBJECT (buttonLaunch), "clicked",
                             GTK_SIGNAL_FUNC (daemonLaunch),
                             NULL);
  gtk_signal_connect        (GTK_OBJECT (buttonConnect), "clicked",
                             GTK_SIGNAL_FUNC (daemonConnect),
                             GTK_OBJECT (connect)  );

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (connect, connect, "connect");
  GLADE_HOOKUP_OBJECT (connect, bonobodock3, "bonobodock3");
  GLADE_HOOKUP_OBJECT (connect, menubarConn_uiinfo[0].widget, "file3");
  GLADE_HOOKUP_OBJECT (connect, file3_menu_uiinfo[0].widget, "quit3");
  GLADE_HOOKUP_OBJECT (connect, menubarConn_uiinfo[1].widget, "help3");
  GLADE_HOOKUP_OBJECT (connect, help3_menu_uiinfo[1].widget, "about4");
  GLADE_HOOKUP_OBJECT (connect, vboxConn, "vboxConn");
  GLADE_HOOKUP_OBJECT (connect, tableConn, "tableConn");
  GLADE_HOOKUP_OBJECT (connect, labelHost, "labelHost");
  GLADE_HOOKUP_OBJECT (connect, labelPort, "labelPort");
  GLADE_HOOKUP_OBJECT (connect, entryPort, "entryPort");
  GLADE_HOOKUP_OBJECT (connect, entryHost, "entryHost");
  GLADE_HOOKUP_OBJECT (connect, hbuttonboxConn, "hbuttonboxConn");
  GLADE_HOOKUP_OBJECT (connect, buttonQuit, "buttonQuit");
  GLADE_HOOKUP_OBJECT (connect, buttonLaunch, "buttonLaunch");
  GLADE_HOOKUP_OBJECT (connect, alignment1, "alignment1");
  GLADE_HOOKUP_OBJECT (connect, hbox2, "hbox2");
  GLADE_HOOKUP_OBJECT (connect, image1, "image1");
  GLADE_HOOKUP_OBJECT (connect, labelLaunch, "labelLaunch");
  GLADE_HOOKUP_OBJECT (connect, buttonConnect, "buttonConnect");
  GLADE_HOOKUP_OBJECT (connect, alignment2, "alignment2");
  GLADE_HOOKUP_OBJECT (connect, hbox3, "hbox3");
  GLADE_HOOKUP_OBJECT (connect, image2, "image2");
  GLADE_HOOKUP_OBJECT (connect, labelConnect, "labelConnect");
  GLADE_HOOKUP_OBJECT_NO_REF (connect, tooltips, "tooltips");

  gconf_engine_unref(gc_engine);
  
  return connect;
}


void
daemonConnect                          (GtkButton       *button,
                                        gpointer         user_data)
{	
	GtkWidget *window = (GtkWidget *)user_data;
	char *host,
	     *aport;
	int port;
	w3lclient *w3lc;

	host = (char *)gtk_entry_get_text(
		GTK_ENTRY(lookup_widget(window,"entryHost")));

	aport = (char *)gtk_entry_get_text(
		GTK_ENTRY(lookup_widget(window,"entryPort")));

	if(!host)
		host = "localhost";

	if(aport == NULL) {
		port = 1030;
		aport = "1030";
	}
	else
		port = atoi(aport);

	{
		GConfEngine* gce = gconf_engine_get_default();
		gconf_engine_set_string(gce,"/apps/w3live/host",host,NULL);
		gconf_engine_set_string(gce,"/apps/w3live/port",aport,NULL);
		gconf_engine_unref(gce);
	}

	if(!(w3lc = init_client(host,port))) {
		gtk_widget_show(create_conn_fail("Could not contact the daemon. Please check the host and port, and try again."));
		return;
	}
	gtk_widget_destroy(window);
	gtk_widget_show(create_appW3Live(w3lc));
}

void
daemonLaunch                           (GtkButton       *button,
                                        gpointer         user_data)
{
	gtk_widget_destroy((GtkWidget *)user_data);
	gtk_widget_show(create_launch());
}
